package com.example.backetballgame.store;

import com.example.backetballgame.event.EventBus;
import com.example.backetballgame.event.FrameEvent;
import com.example.backetballgame.item.Store;
import com.example.backetballgame.item.StorePotion;
import com.example.backetballgame.player.Player;
import com.example.backetballgame.team.Team;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 这个包下面存放缓存信息
 */
public class StoreManage {

    // 运动员信息
    private static List<Player> players = new ArrayList<>();
    private static List<StorePotion> storePotions = new ArrayList<>();

    private static int totalMonty = 2000;

    /**
     * 以下是球队名称
     */
    private static String teamName = "";

    /**
     * 赛季
     */
    private static int season = 0;

    private static int currentSeason = 0;

    private static int points = 0;

    private static List<Player> activePlayers = new ArrayList<>();

    private static Team opponentTeam;

    private static AtomicInteger number = new AtomicInteger(1);

    private static final Store store = new Store();

    public static List<Player> getPlayers() {
        return players;
    }

    public static void savePlayers(List<Player> players) {
        StoreManage.players = players;
    }

    public static String getTeamName() {
        return teamName;
    }

    public static void saveTeamName(String teamName) {
        StoreManage.teamName = teamName;
    }

    public static int getSeason() {
        return season;
    }

    public static void saveSeason(int season) {
        StoreManage.season = season;
    }

    public static int getCurrentSeason() {
        return currentSeason;
    }

    public static void saveCurrentSeason(int currentSeason) {
        StoreManage.currentSeason = currentSeason;
    }

    public static int getTotalMonty() {
        return totalMonty;
    }

    public static void saveTotalMonty(int totalMonty) {
        StoreManage.totalMonty = totalMonty;
    }

    public static List<StorePotion> getStorePotions() {
        return storePotions;
    }

    public static void saveStorePotion(StorePotion storePotions) {
        boolean exists = false;
        for (StorePotion storePotion : StoreManage.storePotions) {
            if (storePotion.getEffect() == storePotions.getEffect()) {
                exists = true;
                storePotion.setQuantity(storePotion.getQuantity() + storePotions.getQuantity());
                break;
            }
        }
        if (!exists) {
            StoreManage.storePotions.add(storePotions);
        }
    }

    public static List<Player> getActivePlayers() {
        return activePlayers;
    }

    public static void saveActivePlayers(List<Player> activePlayers) {
        StoreManage.activePlayers.clear();
        StoreManage.activePlayers.addAll(activePlayers);
    }

    public static Team getOpponentTeam() {
        return opponentTeam;
    }

    public static void saveOpponentTeam(Team opponentTeam) {
        StoreManage.opponentTeam = opponentTeam;
    }

    public static Integer getNumber() {
        return number.getAndIncrement();
    }

    public static int getPoints() {
        return points;
    }

    public static void savePoints(int points) {
        StoreManage.points = points;
    }

    public static Store getStore() {
        return store;
    }

    public static void initConfig(String teamName, String season) {
        saveTeamName(teamName);
        saveSeason(Integer.parseInt(season.trim()));
        saveCurrentSeason(1);
        generateGoods();
    }

    public static boolean isLastWeek(){
        return currentSeason == season;
    }
    public static void nextWeek() {
        generateGoods();
        saveCurrentSeason(getCurrentSeason() + 1);
        for (Player player : players) {
            player.setFatigue(0);
        }
        EventBus.getInstance().publish(new FrameEvent(1));
    }

    private static void generateGoods() {
        //更新商店物品
        store.removeAll();
        for (int i = 0; i < 5; i++) {
            Player player = StoreFactory.createPlayer();
            store.addItem(player);
        }

        for (StorePotion storePotion : StoreFactory.createStorePotion()) {
            store.addItem(storePotion);
        }
    }
}
